#!/bin/bash

echo "+++++++++++++++++++++++ $(cd `dirname $0`; pwd)/$(basename $0) +++++++++++++++++++++++++"

# 当前脚本所在目录
v_d_current=$(cd `dirname $0`; pwd)
# 当前脚本的配置信息
v_f_settings=${v_d_current}/settings
# 当前脚本的环境变量配置
v_env_filename=`ls ${v_d_current} | grep env.sh`
v_f_env=${v_d_current}/${v_env_filename}
# 环境变量文件
v_f_profile=${g_d_profile}/${v_env_filename}

# 检查配置文件是否存在
if [ ! -f ${v_f_settings} ]; then
    echo "settings file does not exist : $v_f_settings"
    exit -1
fi

# 加载配置信息
source ${g_f_util_properties} "load" ${v_f_settings}

# 检查环境变量文件是否存在
if [ ! -f ${v_f_env} ]; then
    echo "env file does not exist : $v_f_env"
    exit -1
fi

# 检查安装目录是否存在
if [ ! -n "$install_dir" ] || [ ! -d ${install_dir} ]; then
    echo "installation directory does not exist : $install_dir"
    exit -1
fi

# 检查安装包是否存在
if [ ! -n "$install_file" ] || [ ! -f ${install_file} ]; then
    echo "installation file does not exist : $install_file"
    exit -1
fi

# 解压安装包到安装目录
tar -zxf ${install_file} -C ${install_dir}

# nexus.properties 配置
\cp ${v_d_current}/nexus.properties ${install_dir}/nexus-*/conf/nexus.properties

# 建立软连接
v_d_home=${install_dir}/nexus
ln -fs ${v_d_home}-* ${v_d_home}

# 设置环境变量
\cp ${v_f_env} ${v_f_profile}
${g_f_util_replace} -s "$v_f_env" -t "$v_f_profile" HOME=${v_d_home}

# 设置开机启动
v_f_service_config=${v_d_current}/nexus-service
v_service="nexus"
v_f_service="/etc/init.d/$v_service"
touch "$v_f_service"
${g_f_util_replace} -s "$v_f_service_config" -t "$v_f_service" HOME=${v_d_home}
chmod 755 "$v_f_service"
chown root "$v_f_service"
chkconfig --add "$v_service"
chkconfig --levels 345 "$v_service" on

source /etc/profile
echo "PATH : $PATH"
echo "NEXUS_HOME : $NEXUS_HOME"
chkconfig --list | grep "$v_service"
exit -1